package com.bjsxt.shopping.util;

import java.sql.SQLException;
import java.util.*;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;

public class UserManager {
	public static List<User> getUsers() {
		List<User> list = new ArrayList<User>();
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			conn = (Connection) DB.getConn();
			stmt = (Statement) DB.getStatement(conn);
			rs = (ResultSet) stmt
					.executeQuery("select * from user order by id desc");
			while (rs.next()) {
				User u = new User();
				u.setId(rs.getInt("ID"));
				u.setUsername(rs.getString("username"));
				u.setPhone(rs.getString("phone"));
				u.setAddr(rs.getString("addr"));
				u.setRdate(rs.getTimestamp("rdate"));
				list.add(u);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.close(rs);
			DB.close(stmt);
			DB.close(conn);
		}
		return list;
	}

	public static void deleteUser(int id) {
		Connection conn = null;
		Statement stmt = null;
		try {
			conn = (Connection) DB.getConn();
			stmt = (Statement) DB.getStatement(conn);
			stmt.executeUpdate("delete from user where id="+id);

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {

			DB.close(stmt);
			DB.close(conn);
		}
	}
}
